﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.SymmetricCryptoSession
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Security.Principal;

namespace Microsoft.InfoCards
{
  internal class SymmetricCryptoSession : CryptoSession
  {
    private SymmetricAlgorithm m_alg;

    public SymmetricCryptoSession(
      Process process,
      DateTime expiration,
      WindowsIdentity identity,
      byte[] key)
      : base(process, expiration, identity, (object) key, CryptoSession.SessionType.Symmetric)
    {
      this.m_alg = (SymmetricAlgorithm) new RijndaelManaged();
      this.m_alg.Key = key;
    }

    protected override void OnDispose()
    {
      this.m_alg.Clear();
      ((IDisposable) this.m_alg).Dispose();
      this.m_alg = (SymmetricAlgorithm) null;
    }

    protected override void OnWrite(BinaryWriter bwriter)
    {
      bwriter.Write(this.m_alg.KeySize);
      bwriter.Write(this.m_alg.BlockSize);
      bwriter.Write(this.m_alg.FeedbackSize);
    }

    public byte[] GenerateDerivedKey(
      string algorithmUri,
      byte[] label,
      byte[] nonce,
      int derivedKeyLength,
      int offset)
    {
      if (algorithmUri == "http://schemas.xmlsoap.org/ws/2005/02/sc/dk/p_sha1" || algorithmUri == "http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512/dk/p_sha1")
        return new Psha1DerivedKeyGenerator(this.m_alg.Key).GenerateDerivedKey(label, nonce, derivedKeyLength, offset);
      throw InfoCardTrace.ThrowHelperWarning((Exception) new InfoCardArgumentException(SR.GetString("ServiceUnsupportedKeyDerivationAlgorithm", (object) algorithmUri)));
    }

    public TransformCryptoSession GetCryptoTransform(
      CipherMode mode,
      PaddingMode padding,
      int feedbackSize,
      SymmetricCryptoSession.Direction direction,
      byte[] iv)
    {
      this.m_alg.Mode = mode;
      this.m_alg.Padding = padding;
      this.m_alg.IV = iv;
      return new TransformCryptoSession(this.ProcessObj, this.Expiration, this.Identity, SymmetricCryptoSession.Direction.Encrypt != direction ? this.m_alg.CreateDecryptor() : this.m_alg.CreateEncryptor());
    }

    public HashCryptoSession GetKeyedHash()
    {
      KeyedHashAlgorithm hash = (KeyedHashAlgorithm) new HMACSHA1(this.m_alg.Key);
      hash.Initialize();
      return new HashCryptoSession(this.ProcessObj, this.Expiration, this.Identity, hash);
    }

    public enum Direction
    {
      Encrypt = 1,
      Decrypt = 2,
    }
  }
}
